package com.mawujun.constant.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在查询CodeType的时候，就缓存下来
 * @author mawujun
 *
 */
public class SingleAppCodeCache {
	private static Map<String,CodeItem> cache=new HashMap<String,CodeItem>();
	/**
	 * 缓存所有的CodeItem,CodeClassService.cacheCode();
	 */
	public static void cacheCodeByCodeClass(List<CodeClass> list){
		SingleAppCodeCache.cache=new HashMap<String,CodeItem>();;
		for(CodeClass codeClass:list){
			if(codeClass.getCodeItemes()!=null){
				for(CodeItem codeItem:codeClass.getCodeItemes()){
					cache.put(codeItem.getCodeClass().getId()+"=="+codeItem.getCode(), codeItem);
				}
			}	
		}
		
	}
	/**
	 * 缓存所有的CodeItem,CodeItemService.cacheCode();
	 */
	public static void cacheCode(List<CodeItem> list){
		SingleAppCodeCache.cache=new HashMap<String,CodeItem>();;
			if(list!=null){
				for(CodeItem codeItem:list){
					cache.put(codeItem.getCodeClass().getId()+"=="+codeItem.getCode(), codeItem);
				}
			}	
	}
	/**
	 * 会有不存在这个codeItem的情况
	 * @param codeClassId
	 * @param code
	 * @return
	 */
	public static CodeItem  get(String codeClassId,String code){
		CodeItem codeItem= cache.get(codeClassId+""+code);
		return codeItem;
	}

}
